package com.itheima.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect //代替：<aop:aspect ref = "advice">
public class Advice {

    /*<bean id="advice" class="com.itheima.advice.Advice"/>

    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.itheima.service.impl.StudentServiceImpl.*(..))"/>
        <aop:aspect ref="advice">
            <aop:around method="around" pointcut-ref="pt"/>
        </aop:aspect>
    </aop:config>*/
    //定义切入点表达式
    @Pointcut("execution(* com.itheima.service.impl.StudentServiceImpl.*(..))")
    public void pt(){}

    //定义aop环绕方法，利用aop进行动态代理
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp){
        Object result = null;
        try {
            long start = System.currentTimeMillis();
            result = pjp.proceed();
            long end = System.currentTimeMillis();
            System.out.println(end-start);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }
}
